/**
 * --------------------------------------
 * FileName: work.1.c
 * Content: 
 *      筛除表中所有再mink和maxk之间的值
 *      的算法。
 * 
 * Auther:  杨则瑞
 * Encode:  utf-8 | CRLF
 * --------------------------------------
 */


#include<stdio.h>
#include<mono_list.h>
#include<debuging.h>
#include<mono_list_int_ext.h>


const char * title = 
"====================================================================== \n"\
" WORK 2 of Chapter 2                                                   \n"\
"---------------------------------------------------------------------- \n"\
" Goal: Filter elements (< maxk and > mink) out of                      \n"\
" a single linked list.                                                 \n"\
"====================================================================== \n";


/**
 * 删除单向表的所有位于mink和maxk之间的所有数据点。
 */
void monoList_ext_filter(monoList_t * self, comparable_t mink, comparable_t maxk)
{
    if(self->_elem_size != sizeof(comparable_t))
    {
        _debug("MisMatched type","WARN");
        return;
    }

    monoList_node_t * p = self->_header;

    while (p->next != NULL)
    {

        if(*(comparable_t*)p->next->data < maxk
        && *(comparable_t*)p->next->data > mink)
        {
            monoList_node_t * d = p->next;
            if(d == NULL) 
            {
                break;
            }
            p->next = p->next->next;
            free(d);
        }
        else
        {
            p = p->next;
        }

    }
}

#ifdef DESKTOP_MODE /*===========================================================*/
int main()
{
    monoList_t mono_list;
    monoList_init(&mono_list,sizeof(comparable_t));

    printf(title);

    printf("Please enter quantity of input numbers, mink, maxk:\n");

    int n, mi, ma;

    // 从stdin读数据数量，最小值和最大值。
    int qod = scanf("%u%u%u%*[^\n]",&n , &mi, &ma);

    if(qod != 3)
    {
        printf("Expects 3 integers, reads %d.\nExit.\n",qod);
        return -1;
    }

    if(n < 0)
    {
        printf("Fault: Negative n.\nExit.\n");
        return -1;
    }

    if(mi >= ma)
    {
        printf("Note: mi >= ma in input, no number will be filtered.\n");
    }

    if(n == 0)
    {
        printf("Note: n is 0, list will be empty\n");
    }

    if(n != 0)
    {
        printf("Please enter integer numbers:\n");
    }


    //读取输入的整数
    monoList_ext_from_file(&mono_list,stdin,n);



    printf("Number of valid data: %d.\n", monoList_length(&mono_list));
    printf("Current State:\n");


    //输出
    monoList_ext_to_file(&mono_list, stdout);
    putchar('\n');

    // 执行算法
    monoList_ext_filter(&mono_list,mi,ma);


    printf("Filtered State:\n");



    //输出
    monoList_ext_to_file(&mono_list, stdout);
    putchar('\n');


    system("pause");

    //析构
    monoList_uninit(&mono_list);

    return 0;
}
#else
int main()
{
    monoList_t mono_list;
    monoList_init(&mono_list,sizeof(comparable_t));



    int n, mi, ma;

    // 从stdin读数据数量，最小值和最大值。
    int qod = scanf("%u%u%u%*[^\n]",&n , &mi, &ma);

    if(qod != 3)
    {
        printf("Expects 3 integers, reads %d.\nExit.\n",qod);
        return -1;
    }

    if(n < 0)
    {
        printf("Fault: Negative n.\nExit.\n");
        return -1;
    }



    //读取输入的整数
    monoList_ext_from_file(&mono_list,stdin,n);


    //输出
    monoList_ext_to_file(&mono_list, stdout);
    putchar('\n');

    // 执行算法
    monoList_ext_filter(&mono_list,mi,ma);



    //输出
    monoList_ext_to_file(&mono_list, stdout);
    putchar('\n');


    //析构
    monoList_uninit(&mono_list);

    return 0;
}

#endif /*========================================================================*/
